﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NewBee.Core.BusinessService;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace WebMvc_Survey.ApiControllers
{
    /// <summary>
    /// 问卷
    /// </summary>
    [Route("api/[controller]")]
    public class Survey_ResultController : Controller
    {
        AgentFactory _AgentFactory = new AgentFactory();

        /// <summary>
        /// 获取未完成问卷
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="formId">问卷表ID</param>
        /// <returns></returns>
        [HttpGet]
        public NewBee.Core.Models.Survey_Result Get([FromQuery]string userId,[FromQuery]string formId)
        {
            return _AgentFactory.Survey_Result.Get(userId, formId);
        }

        /// <summary>
        /// 提交问卷
        /// </summary>
        /// <param name="survey_Result"></param>
        /// <returns></returns>
        [HttpPost]
        public NewBee.Core.Data.Model.SuccessResult Post([FromBody]NewBee.Core.Models.Survey_Result survey_Result)
        {
            NewBee.Core.Data.Model.SuccessResult _SuccessResult = new NewBee.Core.Data.Model.SuccessResult();
            try
            {
                if (survey_Result.Status == 1)
                {
                    int _int = _AgentFactory.Survey_Result.Count("FormID = @FormId And ShareID = @ShareId", new { FormId = survey_Result.FormID, ShareId = survey_Result.ShareID });
                    _AgentFactory.Survey_Share.Update(survey_Result.ShareID, _int);
                }
                    
                _SuccessResult.Success = _AgentFactory.Survey_Result.Edit(survey_Result);
            }
            catch (BusinessException ex)
            {
                _SuccessResult.Message = ex.Message;
            }
            catch (Exception ex)
            {
                _SuccessResult.Message = ex.Message;
            }
            return _SuccessResult;
            
        }

        /// <summary>
        /// 修改奖品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="prize"></param>
        /// <returns></returns>
        [HttpPut, Route("PutPrize")]
        public NewBee.Core.Data.Model.SuccessResult PutPrize([FromForm]Guid id, [FromForm]string prize)
        {
            NewBee.Core.Data.Model.SuccessResult _SuccessResult = new NewBee.Core.Data.Model.SuccessResult();
            try
            {

                _SuccessResult.Success = _AgentFactory.Survey_Result.Update(id, prize);
            }
            catch (BusinessException ex)
            {
                _SuccessResult.Message = ex.Message;
            }
            catch (Exception ex)
            {
                _SuccessResult.Message = ex.Message;
            }
            return _SuccessResult;
        }

        /// <summary>
        /// 修改手机号
        /// </summary>
        /// <param name="id"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        [HttpPut,HttpGet, Route("PutRemark")]
        public NewBee.Core.Data.Model.SuccessResult PutRemark([FromQuery]Guid id, [FromQuery]string remark)
        {
            NewBee.Core.Data.Model.SuccessResult _SuccessResult = new NewBee.Core.Data.Model.SuccessResult();
            try
            {
                _SuccessResult.Success = _AgentFactory.Survey_Result.UpdateRemark(id, remark);
                Response.Redirect("/Home/PrizeResult");
            }
            catch (BusinessException ex)
            {
                _SuccessResult.Message = ex.Message;
            }
            catch (Exception ex)
            {
                _SuccessResult.Message = ex.Message;
            }
            return _SuccessResult;
        }
    }
}
